package com.store.pet.petstore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextPaint;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.bean.MessageBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.store.pet.petstore.activity.PetListActivity.BUY_SUCCESS;

public class EditAddressActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.address_et)
    EditText addressEt;

    private int buyer = 0;  //买家id

    private int petid = 0;  //宠物id


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_edit_address;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出
    }

    @Override
    public void loadData() {
        buyer = getIntent().getIntExtra("buyer",0);
        petid = getIntent().getIntExtra("petid",0);
    }

    @OnClick(R.id.determine)
    public void onViewClicked() {
        //下单
        String address = addressEt.getText().toString();
        if(!address.equals("")){
            Map<String,Object> map = new HashMap<>();
            map.put("address",address);
            map.put("buyer",buyer);
            map.put("petid",petid);
            OKHttpUtils.requestData(Constant.BUY, map, new OKHttpUtils.ServerCallBack() {
                @Override
                public void onSuccess(String data) {
                    MessageBean msg = GsonUtils.parseMessage(data);
                    if(msg.getCode() == 102){
                        setResult(BUY_SUCCESS,new Intent());
                        finish();
                        hideKeyboard(getCurrentFocus()); //隐藏键盘
                    }
                    ToastUtils.showShort(msg.getMsg());
                }

                @Override
                public void onFailed(String message) {
                    ToastUtils.showShort("网络出错");
                }
            });
        }else{
            ToastUtils.showShort("请输入地址");
        }
    }
}
